package com.example.asus.yuejian_10;

import android.content.SharedPreferences;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;


import com.example.asus.yuejian_10.Login.LoginFragment;
import com.example.asus.yuejian_10.base.BaseActivity;
import com.example.asus.yuejian_10.bean.CCourse;
import com.example.asus.yuejian_10.fragment.MainFragment;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends BaseActivity {

    private String fack = "{\"id\":\"2\",\"course_name\":\"课程1\",\"course_price\":\"99\",\"introduction\":\"&lt;p&gt;课程1课程1课程1课程1课程1课程1课程1&lt;\\/p&gt;\",\"notice\":\"课程1\",\"bmnotice\":\"课程1\",\"start_date\":\"2016-08-09 00:00:00\",\"end_date\":\"2016-10-09 00:00:00\",\"thumb\":\"\",\"degree\":\"3\"}]";

    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;

    private LoginFragment loginFragment;
    private MainFragment mainFragment;

    private SharedPreferences sp;
    private final String fileName = "food";//文件名   自动加.XML
    private final int mode=MODE_PRIVATE;//访问权限


    public void test(){
//        try {
//            JSONArray myJsonArray =new JSONArray(fack);
//            for(int i=0 ; i < myJsonArray.length() ;i++){
//                JSONObject myjObject = myJsonArray.getJSONObject(i);
//                Gson g = new Gson();
//                CCourse c = g.fromJson(myjObject,CCourse.class);
//            }
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
        Gson g = new Gson();
        List<CCourse> list = new ArrayList<>();
        list = g.fromJson(fack,new TypeToken<List<CCourse>>(){}.getType());
        Log.d("cainima", "test: " + list.size());
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sp = getSharedPreferences(fileName,
                mode);
        fragmentManager = getSupportFragmentManager();
        showMainFragment();
/*        //判断是否登录,如果登录，进入主界面
        //否则，显示登录界面
        boolean loginState = getLoginState();
        if(loginState){
            //直接进入主页面
            showMainFragment();
            //   userId = readUserId();//读取已存储的用户的Id
        }
        else{
            //显示登录
            showLoginFragment();
        }*/
    }

    private void showLoginFragment(){
        transaction=fragmentManager.beginTransaction();

        loginFragment=new LoginFragment();
        transaction.add(R.id.fragment_holder,loginFragment);

        transaction.commit();
    }

    private void showMainFragment(){
        transaction=fragmentManager.beginTransaction();

        mainFragment=new MainFragment();
        transaction.add(R.id.fragment_holder,mainFragment);

        transaction.commit();
    }

   /* public void replaceLoginFragment(){
        mainFragment=new MainFragment();
        transaction=fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_holder,mainFragment);
        transaction.commit();
    }


    private boolean getLoginState(){
        //  String userId = getSavedUserId();
        userId = getSavedUserId();
        return !userId.equals("-1");//-1未登录
    }

    public String getSavedUserId(){
        String userId = sp.getString("user_id","-1");//第二个参数   未读到数据时读的数据！
        return userId;
    }

    public void setSavedUserId(String userId){
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("user_id",userId);
        editor.commit();
    }*/

}
